package com.example.demotest.demos.bens;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SshCommand {

    // 获取服务器信息
//    String serverName = executeCommand(session, "hostname");
//    String internalIP = executeCommand(session, "hostname -I | awk '{print $1}'");
//    String cpuUsage = executeCommand(session, "top -bn1 | grep 'Cpu(s)' | awk '{print $2 + $4}'");
//    String memoryUsage = executeCommand(session, "free | awk 'NR==2 {print $3/$2*100}'");
//    String diskUsage = executeCommand(session, "df -h | awk '$NF==\"/\"{print $5}'");

    /**
     * 获取磁盘信息
     *
     * @param sshClient
     * @param exceedUsageRate
     * @return 使用率：usageRate 超过传入阈值的使用率情况：exceedUsageRate
     */
    public static JSONObject getDiskInfo(SSHClient sshClient, Double exceedUsageRate) {
        JSONObject jsonObject = new JSONObject();
        String commond5 = "df -h";
        System.out.println("开始对主机" + sshClient.getHost() + "进行磁盘情况扫描！");
        String ret5 = sshClient.sendCmd(commond5).trim();
        String[] split5 = ret5.split("\n");
        if (split5.length > 0) {
            List<String> str = new ArrayList<>();
            List<String> str2 = new ArrayList<>();
            for (int i = 1; i < split5.length; i++) {
                String[] split6 = split5[i].split("\\s+");
                str.add(split6[5] + "; 总容量:"+split6[1]+";目录下使用率：" + split6[4]);
                System.out.println(split6[5] + "; 总容量:"+split6[1]+";目录下使用率：" + split6[4]);
                if (exceedUsageRate <= Double.parseDouble(split6[4].replace("%", ""))) {
                    str2.add(split6[5] + "; 总容量:"+split6[1]+";目录下使用率：" + split6[4]);
                }
            }
            System.out.println("已结束对主机" + sshClient.getHost() + "进行磁盘情况扫描！");
            jsonObject.put("usageRate", str);
            jsonObject.put("exceedUsageRate", str2);
        }
        return jsonObject;
    }

    /**
     * 获取内存信息
     *
     * @param sshClient
     * @param exceedUsageRate
     * @return 使用率：usageRate 超过传入阈值的使用率情况：exceedUsageRate
     */
    public static JSONObject getMemoryInfo(SSHClient sshClient, Double exceedUsageRate) {
        JSONObject jsonObject = new JSONObject();
        String commond5 = "free -g | awk 'NR==2 {print $2,$3,$4,$3/$2*100}'";
        System.out.println("开始对主机" + sshClient.getHost() + "进行内存情况扫描！");
        String ret5 = sshClient.sendCmd(commond5).trim();

        String[] split5 = ret5.split("\\s+");
        if (split5.length > 0) {
            List<String> str = new ArrayList<>();
            List<String> str2 = new ArrayList<>();
            str.add("总内存：" + split5[0]);
            str.add("已使用：" + split5[1]);
            str.add("剩余：" + split5[2]);
            str.add("使用率：" + split5[3]);
            if (exceedUsageRate <= Double.parseDouble(split5[3])) {
                str2.add("内存使用率已超过阈值：" + split5[3] + "%");
            }
            jsonObject.put("usageRate", str);
            jsonObject.put("exceedUsageRate", str2);
        }
        System.out.println("已结束对主机" + sshClient.getHost() + "进行内存情况扫描！");
        return jsonObject;
    }

    /**
     * 获取cpu信息
     *
     * @param sshClient
     * @param exceedUsageRate
     * @return 使用率：usageRate 超过传入阈值的使用率情况：exceedUsageRate
     */
    public static JSONObject getCpuInfo(SSHClient sshClient, Double exceedUsageRate) {
        JSONObject jsonObject = new JSONObject();
        String commond5 = "top -bn1 | grep 'Cpu(s)' | awk '{print $2 + $4}'";
        System.out.println("开始对主机" + sshClient.getHost() + "进行CPU情况扫描！");
        String ret5 = sshClient.sendCmd(commond5).trim();
        if (!StringUtils.isEmpty(ret5)) {
            List<String> str = new ArrayList<>();
            List<String> str2 = new ArrayList<>();
            str.add("CPU使用率" + ret5);
            if (exceedUsageRate <= Double.parseDouble(ret5)) {
                str2.add("CPU使用率已超过阈值：" + ret5 + "%");
            }
            jsonObject.put("usageRate", str);
            jsonObject.put("exceedUsageRate", str2);
        }
        System.out.println("已结束对主机" + sshClient.getHost() + "进行CPU情况扫描！");
        return jsonObject;
    }

    /**
     * 获取操作系统类型和版本信息
     *
     * @param sshClient
     * @return msg描述
     */
    public static JSONObject getSystemInfo(SSHClient sshClient) {
        JSONObject jsonObject = new JSONObject();
        String commond5 = "hostnamectl | awk 'NR==7 {print $3$4$5}'";
        System.out.println("开始对主机" + sshClient.getHost() + "进行系统信息情况扫描！");
        String ret5 = sshClient.sendCmd(commond5).trim();
        if ("".equals(ret5)) {
            return jsonObject;
        }
        if (!StringUtils.isEmpty(ret5)) {
            jsonObject.put("msg", ret5);
        }
        System.out.println("已结束对主机" + sshClient.getHost() + "进行系统信息情况扫描！");
        return jsonObject;
    }

    /**
     * 获取服务器厂商和序列号
     *
     * @param sshClient
     * @return msg描述
     */
    public static JSONObject getManufacturerInfo(SSHClient sshClient) {
        JSONObject jsonObject = new JSONObject();
        String commond5 = "dmidecode -t system | awk 'NR==8 {print $3$4$5$6$7} NR==10 {print $3}'";
        System.out.println("开始对主机" + sshClient.getHost() + "进行服务器厂商和序列号情况扫描！");
        String ret5 = sshClient.sendCmd(commond5).trim();
        if ("".equals(ret5)) {
            return jsonObject;
        }
        String[] split5 = ret5.split("\\s+");
        if (split5.length > 0) {
            List<String> str = new ArrayList<>();
            for (int i = 0; i < split5.length; i++) {
                if(i==0){
                    str.add("厂商及型号：" + split5[i]);
                }else if(i==1){
                    str.add("序列号：" + split5[i]);
                }else {
                    break;
                }
            }

            jsonObject.put("manufacturer", str);
        }
        System.out.println("已结束对主机" + sshClient.getHost() + "进行服务器厂商和序列号情况扫描！");
        return jsonObject;
    }
}
